//
//  QDChangeProvinceVC.m
//  QuickDoctor
//
//  Created by 李伟 on 2016/12/21.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDChangeProvinceVC.h"
#import "QDCityModel.h"
#import "QDCityTableViewCell.h"
#import "QDChangeCityVC.h"
static NSString* cellID = @"cellID";

@interface QDChangeProvinceVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray<QDCityModel*> *modelList;
@end

@implementation QDChangeProvinceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self setupUI];
}
#pragma mark
#pragma mark - 获取数据
- (void)loadData
{
    // 1. 获取plist文件的地址
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
    // 2. 加载Plist字典集合
    NSArray *dictArr = [NSArray arrayWithContentsOfFile:filePath];
    
    // 3. 遍历字典集合，创建模型对象，添加到一个可变数组
    NSMutableArray *modelArrM = [NSMutableArray array];
    
    // 4.外层循环
    for (NSDictionary *dict in dictArr) {
        
        QDCityModel *model = [QDCityModel new];
        model.cities = dict[@"cities"];
        model.name = dict[@"name"];
        [modelArrM addObject:model];
        
    }
    self.modelList = modelArrM.copy;
}

#pragma mark
#pragma mark - 搭建界面
- (void)setupUI
{
    UITableView *tableView = [[UITableView alloc]init];
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
}


#pragma mark
#pragma mark - TBDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelList.count;
}

- (QDCityTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    QDCityTableViewCell *cell = [[QDCityTableViewCell alloc]init];
    
    QDCityModel * model = self.modelList[indexPath.row];
    
    cell.name.text = model.name;
    
    return cell;
}

#pragma mark
#pragma mark - TBDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    QDChangeCityVC *cityVC = [[QDChangeCityVC alloc]init];
    cityVC.title = @"城市";
    cityVC.modelList = self.modelList;
    cityVC.tempIndex = indexPath;
    [self.navigationController pushViewController:cityVC animated:YES];
}
@end
